﻿using System.Linq;

namespace OneAdmin.Controllers
{
    using System.Web.Mvc;

    /// <summary>
    /// 控制器父类
    /// </summary>
    public class BaseController : Controller
    {
        /// <summary>
        /// 获取model错误信息
        /// </summary>
        /// <returns></returns>
        protected string ModelErrorMs()
        {
            var modelState = this.ModelState.Values.FirstOrDefault();
            if (modelState != null)
            {
                var error = modelState.Errors.FirstOrDefault();
                if (error != null)
                {
                    return error.ErrorMessage;
                }
            }

            return string.Empty;
        }

        /// <summary>
        /// 获取当前控制器名
        /// </summary>
        protected string GetControllerName
        {
            get
            {
                return this.RouteData.Values["controller"].ToString();
            }
        }

        /// <summary>
        /// 获取当前操作名
        /// </summary>
        protected string GetActionName
        {
            get
            {
                return this.RouteData.Values["action"].ToString();
            }
        }

        protected string GetHost
        {
            get
            {
                return this.Request.Url.Host + "/" + this.GetControllerName + "/" + this.GetActionName;
            }
        }
    }
}
